package com.koron.common.web.log.controller;


import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.business.log.entity.po.LogLoginPo;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.web.log.bean.po.Log;
import com.koron.common.web.log.bean.po.LogQuery;
import com.koron.common.web.log.service.LogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 操作日志(Log)表控制层
 *
 * @author jzp
 * @since 2022-03-18 08:38:26
 */
@Api(tags = "操作日志接口")
@RestController
@RequestMapping("log")
public class LogController {
    /**
     * 服务对象
     */
    @Autowired
    private LogService logService;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    @ApiOperation(value = "根据业务id查询")
    @GetMapping
    public Response<List<Log>> selectOne(String businessId) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), logService, "listByBusinessId", List.class, businessId));
    }

    @ApiOperation(value = "保存或更新数据")
    @PostMapping
    public Response<String> save(@RequestBody Log log) {
        ADOConnection.runTask(getCurrDs(), logService, "save", Boolean.class, log);
        return Response.ok(log.getId());
    }

    @ApiOperation(value = "批量保存")
    @PostMapping("batch")
    public Response<String> save(@RequestBody List<Log> logs) {
        ADOConnection.runTask(getCurrDs(), logService, "saveBatch", Boolean.class, logs);
        return Response.ok();
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("page")
    public Response<PageInfo> page(LogQuery log, PageQuery pageQuery) {
        PageInfo pageInfo = ADOConnection.runTask(getCurrDs(), logService, "page", PageInfo.class, pageQuery,log);
        return Response.ok(pageInfo);
    }

    @ApiOperation(value = "保存用户登录信息")
    @PostMapping("saveLogLogin")
    public Response<String> saveLogLogin(@RequestBody LogLoginPo loginPo) {
        Integer result = ADOConnection.runTask(getCurrDs(), logService, "saveLogLogin", Integer.class, loginPo);
        return Response.ok(result+"");
    }

    @ApiOperation(value = "分页查询用户登录信息")
    @GetMapping("logLogin/page")
    public Response<PageInfo> logLoginPage(LogQuery log, PageQuery pageQuery) {
        PageInfo pageInfo = ADOConnection.runTask(getCurrDs(), logService, "logLoginPage", PageInfo.class, pageQuery,log);
        return Response.ok(pageInfo);
    }


}
